﻿package igz.fleaxy.ui.preloader {
	
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import igz.fleaxy.util.ObjectUtil;
	
	/**
	 * Clase que crea un preloader linear simple
	 *
	 * @param	p_settings : Object = { "backLayer": displayObject [Optional]
	 * 								  , "frontLayer": displayObject
	 * 								  , "textEnable": Boolean
	 * 								  , "textSize": uint
	 * 								  , "textColor": uint
	 * 								  , "textDisplace": Point
	 * 								  , "type": LinearPreloaderType }
	 *
	 * @example LinearPreloaderUi( { "backLayer":  displayObject_1
	 * 							   , "frontLayer": displayObject_2
	 * 							   , "textEnable": false
	 * 							   , "textSize": 15
	 * 							   , "textColor": 0xFFFFFF
	 * 							   , "textDisplace": new Point(5, -25);
	 * 							   , "type": LinearPreloaderType.$HORIZONTAL
	 * 							   }
	 * 							 );
	 */

	public class LinearPreloaderUi extends Sprite {
		
		protected var _BackLayer   : Sprite;
		protected var _FrontLayer  : Sprite;
		protected var _MaskLayer   : Shape;
		protected var _CounterText : TextField;
		
		protected var _Settings : Object = { "backLayer": null
										,  "frontLayer": null
										,  "backColor": 0xFFFFFF
										,  "frontColor": 0x000000
										,  "textEnable": false
										,  "textSize":  15
										,  "textColor": 0xFFFFFF
										,  "textDisplace": new Point()
										,  "type": LinearPreloaderType.$HORIZONTAL
										};
		
		// Constructora
		public function LinearPreloaderUi( p_settings:Object = null ) {
			
			// Combinamos opciones
			ObjectUtil.$Merge( p_settings, _Settings );
			
			// Capa frontal
			_FrontLayer = new Sprite();
			addChild( _FrontLayer );
			
			if ( _Settings["frontLayer"] == null ) {
				_Load_DefaultPreloader();
			}
			else {
				_Load_CustomPreloader();
			}
			
			// Máscara
			_MaskLayer = new Shape();
			addChild( _MaskLayer );
			
			_FrontLayer.mask = _MaskLayer;
		}
	
		
		// Método que establece un cargador por defecto
		protected function _Load_DefaultPreloader() : void {
			
			// Capa trasera
			_BackLayer = new Sprite();
			_BackLayer.graphics.lineStyle( 1, _Settings["backColor"], 1, true );
			_BackLayer.graphics.beginFill( _Settings["backColor"], 1 );
			_BackLayer.graphics.drawRect( 0, 0, 200, 25 );
			_BackLayer.graphics.endFill();
			addChild( _BackLayer );
			
			// Capa frontal
			_FrontLayer.graphics.lineStyle( 1, _Settings["backColor"], 1, true );
			_FrontLayer.graphics.beginFill( _Settings["frontColor"], 1 );
			_FrontLayer.graphics.drawRect( 0, 0, 200, 25 );
			_FrontLayer.graphics.endFill();
			
			// Cambio de orden entre capas
			swapChildren( _BackLayer, _FrontLayer );
		
			// Contador de progresion
			if ( _Settings["textEnable"] ) {
			_Create_CounterText();
			}
		}
		
		
		
		// Método que establece un cargador personalizado
		protected function _Load_CustomPreloader() : void {
						
			// Capa trasera
			if ( _Settings["backLayer"] != null ) {
				_BackLayer = new Sprite();
				_BackLayer.addChild( _Settings["backLayer"] );
				addChild( _BackLayer );
				swapChildren( _BackLayer, _FrontLayer );
			}
			
			// Capa frontal
			_FrontLayer.addChild( _Settings["frontLayer"] );
			
			// Contador de completado
			if ( _Settings["textEnable"] ) {
				_Create_CounterText();
			}

		}
		
		
		// Método que establece un contador prar el cargador
		protected function _Create_CounterText() : void {
			_CounterText = new TextField();
			_CounterText.defaultTextFormat = new TextFormat( "Verdana", _Settings["textSize"], _Settings["textColor"] );
			_CounterText.selectable = _CounterText.multiline = false;
			addChild( _CounterText );
			
			_SetPosition_CounterText();
		}
		
		// Metodo que asigna un valor al cargador
		/**
		 *
		 * @param	p_value: Porcentaje de carga 0-100
		 */
		public function $SetValue( p_value:uint ) : void {
			_UpdatePreloader( p_value );
		}
		
		protected function _UpdatePreloader( p_value:uint ) : void {
			
			_MaskLayer.graphics.clear();
			_MaskLayer.graphics.beginFill( 0xFFFFFF, 0);
			
			switch ( _Settings["type"] ) {
				case LinearPreloaderType.$HORIZONTAL : _DespX( p_value ); break;
				case LinearPreloaderType.$VERTICAL   : _DespY( p_value ); break;
			}
			
			_MaskLayer.graphics.endFill();
			
			if ( _CounterText ) {
				_Update_CounterText( p_value );
			}
		}
		
		protected function _Update_CounterText( p_value:uint ) : void {
			_CounterText.htmlText = String( p_value + " % ");
			_SetPosition_CounterText();
		}
		
		protected function _DespX( p_value:uint ) : void {
			_MaskLayer.graphics.drawRect( 0, 0, ( p_value * _FrontLayer.width ) / 100, _FrontLayer.height );
		}
		
		protected function _DespY( p_value:uint ) : void {
			_MaskLayer.graphics.drawRect( 0,  _FrontLayer.height - ( p_value * _FrontLayer.height ) / 100, _FrontLayer.width, _FrontLayer.height );
		}
		
		protected function _SetPosition_CounterText() : void {
			_CounterText.x = ( _FrontLayer.width  - _CounterText.textWidth ) * 0.5 + _Settings["textDisplace"].x;
			_CounterText.y = ( _FrontLayer.height - _CounterText.textHeight ) * 0.5 + _Settings["textDisplace"].y;
		}
		
	}

}